package com.gxar.marketdelivery.shared.utils;

import com.gxar.marketdelivery.interfaces.web.result.IBaseResponse;
import com.gxar.marketdelivery.interfaces.web.result.IResponseStatusMsg;
import com.gxar.marketdelivery.shared.meta.exception.APIRuntimeException;

import java.util.Collection;
import java.util.Map;

/**
 * @author nice
 * @version 1.0
 * @created 2023/7/21.
 */
public class AssertUtils {

    //支持message带{},传参数
    public static void isNull(Object object, IResponseStatusMsg resultCode, String message, Object... args) {
        if (object != null) {
            throw new APIRuntimeException(resultCode, StringSpliceUtils.splice(message, args));
        }
    }

    //API错误异常
    public static void isNull(Object object, IResponseStatusMsg resultCode) {
        if (object != null) {
            throw new APIRuntimeException(resultCode);
        }
    }

    public static void isNull(Object object, IResponseStatusMsg resultCode, String message) {
        if (object != null) {
            throw new APIRuntimeException(resultCode, message);
        }
    }

    //支持message带{},传参数
    public static void notNull(Object object, IResponseStatusMsg resultCode, String message, Object... args) {
        if (object == null) {
            throw new APIRuntimeException(resultCode, StringSpliceUtils.splice(message, args));
        }
    }

    public static void notNull(Object object, IResponseStatusMsg resultCode) {
        if (object == null) {
            throw new APIRuntimeException(resultCode);
        }
    }

    public static void notNull(Object object, IResponseStatusMsg resultCode, String message) {
        if (object == null) {
            throw new APIRuntimeException(resultCode, message);
        }
    }

    public static void notNull(Object object, Integer status, String message) {
        if (object == null) {
            throw new APIRuntimeException(status, message);
        }
    }

    //支持message带{},传参数
    public static void isTrue(boolean expression, IResponseStatusMsg resultCode, String message, Object... args) {
        if (!expression) {
            throw new APIRuntimeException(resultCode, StringSpliceUtils.splice(message, args));
        }
    }

    public static void isTrue(boolean expression, IResponseStatusMsg resultCode) {
        if (!expression) {
            throw new APIRuntimeException(resultCode);
        }
    }

    public static void isTrue(boolean expression, IResponseStatusMsg resultCode, String message) {
        if (!expression) {
            throw new APIRuntimeException(resultCode, message);
        }
    }

    public static void isTrue(boolean expression, Integer status, String message) {
        if (!expression) {
            throw new APIRuntimeException(status, message);
        }
    }

    public static <T> void notEmpty(Collection<T> collection, IResponseStatusMsg resultCode) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new APIRuntimeException(resultCode);
        }
    }

    public static <T> void notEmpty(Collection<T> collection, IResponseStatusMsg resultCode, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new APIRuntimeException(resultCode, message);
        }
    }

    public static <T> void notEmpty(Collection<T> collection, Integer status, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new APIRuntimeException(status, message);
        }
    }

    public static <K, V> void notEmpty(Map<K, V> map, IResponseStatusMsg resultCode) {
        if (CollectionUtils.isEmpty(map)) {
            throw new APIRuntimeException(resultCode);
        }
    }

    public static <K, V> void notEmpty(Map<K, V> map, IResponseStatusMsg resultCode, String message) {
        if (CollectionUtils.isEmpty(map)) {
            throw new APIRuntimeException(resultCode, message);
        }
    }

    public static <K, V> void notEmpty(Map<K, V> map, Integer status, String message) {
        if (CollectionUtils.isEmpty(map)) {
            throw new APIRuntimeException(status, message);
        }
    }

    public static void notEmpty(Object[] array, IResponseStatusMsg resultCode) {
        if (ArrayUtils.isEmpty(array)) {
            throw new APIRuntimeException(resultCode);
        }
    }

    public static void notEmpty(Object[] array, IResponseStatusMsg resultCode, String message) {
        if (ArrayUtils.isEmpty(array)) {
            throw new APIRuntimeException(resultCode, message);
        }
    }

    public static void notEmpty(Object[] array, Integer status, String message) {
        if (ArrayUtils.isEmpty(array)) {
            throw new APIRuntimeException(status, message);
        }
    }

    public static void notBlank(final CharSequence cs, IResponseStatusMsg resultCode) {
        if (StringUtils.isBlank(cs)) {
            throw new APIRuntimeException(resultCode);
        }
    }

    public static void notBlank(final CharSequence cs, IResponseStatusMsg resultCode, String message) {
        if (StringUtils.isBlank(cs)) {
            throw new APIRuntimeException(resultCode, message);
        }
    }

    public static void notBlank(final CharSequence cs, Integer status, String message) {
        if (StringUtils.isBlank(cs)) {
            throw new APIRuntimeException(status, message);
        }
    }

    public static <T> void isEmpty(Collection<T> collection, IResponseStatusMsg resultCode) {
        if (CollectionUtils.isNotEmpty(collection)) {
            throw new APIRuntimeException(resultCode);
        }
    }

    public static <T> void isEmpty(Collection<T> collection, IResponseStatusMsg resultCode, String message) {
        if (CollectionUtils.isNotEmpty(collection)) {
            throw new APIRuntimeException(resultCode, message);
        }
    }

    public static <T> void isEmpty(Collection<T> collection, Integer status, String message) {
        if (CollectionUtils.isNotEmpty(collection)) {
            throw new APIRuntimeException(status, message);
        }
    }

    public static <K, V> void isEmpty(Map<K, V> map, IResponseStatusMsg resultCode) {
        if (CollectionUtils.isNotEmpty(map)) {
            throw new APIRuntimeException(resultCode);
        }
    }

    public static <K, V> void isEmpty(Map<K, V> map, IResponseStatusMsg resultCode, String message) {
        if (CollectionUtils.isNotEmpty(map)) {
            throw new APIRuntimeException(resultCode, message);
        }
    }

    public static <K, V> void isEmpty(Map<K, V> map, Integer status, String message) {
        if (CollectionUtils.isNotEmpty(map)) {
            throw new APIRuntimeException(status, message);
        }
    }

    public static void isEmpty(Object[] array, IResponseStatusMsg resultCode) {
        if (ArrayUtils.isNotEmpty(array)) {
            throw new APIRuntimeException(resultCode);
        }
    }

    public static void isEmpty(Object[] array, IResponseStatusMsg resultCode, String message) {
        if (ArrayUtils.isNotEmpty(array)) {
            throw new APIRuntimeException(resultCode, message);
        }
    }

    public static void isEmpty(Object[] array, Integer status, String message) {
        if (ArrayUtils.isNotEmpty(array)) {
            throw new APIRuntimeException(status, message);
        }
    }

    public static void isSuccess(IBaseResponse baseResponse) {
        notNull(baseResponse, IResponseStatusMsg.APIEnum.FAILED);
        if (!success(baseResponse)) {
            throw new APIRuntimeException(baseResponse.getStatus(), baseResponse.getMessage());
        }
    }

    public static void isSuccess(IBaseResponse baseResponse, IResponseStatusMsg resultCode) {
        notNull(baseResponse, resultCode);
        if (!success(baseResponse)) {
            throw new APIRuntimeException(resultCode);
        }
    }

    public static void isSuccess(IBaseResponse baseResponse, IResponseStatusMsg resultCode, String message) {
        notNull(baseResponse, resultCode, message);
        if (!success(baseResponse)) {
            throw new APIRuntimeException(resultCode, message);
        }
    }

    public static void isSuccess(IBaseResponse baseResponse, String message) {
        notNull(baseResponse, IResponseStatusMsg.APIEnum.FAILED, message);
        if (!success(baseResponse)) {
            throw new APIRuntimeException(baseResponse.getStatus(), message);
        }
    }

    private static boolean success(IBaseResponse baseResponse) {
        return IResponseStatusMsg.APIEnum.SUCCESS.getCode().equals(baseResponse.getStatus());
    }

    public static void equal(Integer val1, Integer val2, IResponseStatusMsg resultCode) {
        if (val1 == null ? val2 != null : !val1.equals(val2)) {
            throw new APIRuntimeException(resultCode);
        }
    }

    public static void equal(Integer val1, Integer val2, IResponseStatusMsg resultCode, String message) {
        if (val1 == null ? val2 != null : !val1.equals(val2)) {
            throw new APIRuntimeException(resultCode, message);
        }
    }

    public static void equal(Object val1, Object val2, IResponseStatusMsg resultCode) {
        if (val1 == null ? val2 != null : !val1.equals(val2)) {
            throw new APIRuntimeException(resultCode);
        }
    }

    public static void equal(Object val1, Object val2, IResponseStatusMsg resultCode, String message) {
        if (val1 == null ? val2 != null : !val1.equals(val2)) {
            throw new APIRuntimeException(resultCode, message);
        }
    }

    public static <T> void notContains(Collection<T> c, T e, IResponseStatusMsg resultCode) {
        if (CollectionUtils.isEmpty(c)) {
            return;
        }

        if (c.contains(e)) {
            throw new APIRuntimeException(resultCode);
        }
    }

    public static <T> void notContains(Collection<T> c, T e, IResponseStatusMsg resultCode, String message) {
        if (CollectionUtils.isEmpty(c)) {
            return;
        }

        if (c.contains(e)) {
            throw new APIRuntimeException(resultCode, message);
        }
    }
}
